import { TABLE_EDIT_TYPE } from '@/utils/constants'

interface ContainerWidgetSchema {
  type: string
  alias: string
  category: 'container'
  icon: string
  commonFlag?: boolean
  widgetList?: any[]
  merged?: boolean
  options: {
    name: string
    label?: string
    hidden?: boolean
    customClass?: string
    [key: string]: any
  }
}

export const containers = [
  {
    type: 'grid',
    alias: 'grid', //组件别名，可以自定义
    category: 'container',
    icon: 'grid',
    commonFlag: true,
    cols: [],
    options: {
      name: '',
      hidden: false,
      gutter: 12,
      gridItems: [],
      colHeight: 60, //栅格列统一高度属性，用于解决栅格列设置响应式布局浮动后被挂住的问题！！
      customClass: '', //自定义css类名
    },
  },
  {
    type: 'grid-col',
    category: 'container',
    icon: 'grid-col',
    internal: true,
    widgetList: [],
    options: {
      name: '',
      hidden: false,
      customClass: '', //自定义css类名
      containerStyle: {
        display: 'flex',
        flexDirection: 'row',
        justifyContent: 'flex-start',
        alignItems: 'stretch',
      },
    },
  },

  {
    type: 'grid-adv',
    category: 'container',
    icon: 'sub-form',
    commonFlag: true,
    cols: [],
    options: {
      name: '',
      hidden: false,
      customClass: '', //自定义css类名
      gridAdvStyle: {
        gridAutoFlow: 'row',
        gridTemplateRows: '1fr',
        gridTemplateColumns: '1fr',
        gridRowGap: '10px',
        gridColumnGap: '10px',
      },
    },
  },

  {
    type: 'grid-adv-col',
    category: 'container',
    icon: 'grid-col',
    internal: true,
    widgetList: [],
    options: {
      name: '',
      hidden: false,
      customClass: '', //自定义css类名
      containerStyle: {
        display: 'flex',
        flexDirection: 'row',
        justifyContent: 'flex-start',
        alignItems: 'stretch',
      },
      gridAdvColStyle: {
        gridRowStart: '',
        gridRowEnd: '',
        gridColumnStart: '',
        gridColumnEnd: '',
      },
    },
  },

  {
    type: 'table',
    category: 'container',
    icon: 'table',
    rows: [],
    options: {
      name: '',
      hidden: false,
      customClass: '', //自定义css类名
    },
  },

  {
    type: 'tab',
    category: 'container',
    icon: 'tab',
    commonFlag: true,
    //displayType: 'border-card',
    tabs: [],
    options: {
      name: '',
      tabType: 'border-card',
      tabPosition: 'top',
      hidden: false,
      customClass: '', //自定义css类名
      onTabClick: '', //tab被选中时触发
    },
  },

  {
    type: 'sub-form',
    category: 'container',
    icon: 'sub-form',
    commonFlag: true,
    widgetList: [],
    options: {
      name: '',
      label: '',
      showBlankRow: true,
      showRowNumber: true,
      labelAlign: 'label-center-align',
      hidden: false,
      disabled: false,
      actionColumnPosition: 'left', //操作按钮列位置
      customClass: '', //自定义css类名
      onSubFormRowAdd: '',
      onSubFormRowInsert: '',
      onSubFormRowDelete: '',
      onSubFormRowChange: '',
    },
  },

  {
    type: 'grid-sub-form',
    category: 'container',
    icon: 'grid-sub-form',
    commonFlag: true,
    widgetList: [],
    options: {
      name: '',
      label: '',
      showBlankRow: true,
      showRowNumber: true,
      hidden: false,
      disabled: false,
      actionColumnPosition: 'left', //操作按钮列位置
      customClass: '', //自定义css类名
      onSubFormRowAdd: '',
      onSubFormRowInsert: '',
      onSubFormRowDelete: '',
      onSubFormRowChange: '',
    },
  },
  {
    type: 'table-cell',
    category: 'container',
    icon: 'table-cell',
    internal: true,
    widgetList: [],
    merged: false,
    options: {
      name: '',
      cellWidth: '',
      cellHeight: '',
      colspan: 1,
      rowspan: 1,
      wordBreak: false, //是否自动换行
      customClass: '', //自定义css类名
    },
  },

  {
    type: 'tab-pane',
    category: 'container',
    icon: 'tab-pane',
    internal: true,
    widgetList: [],
    options: {
      name: '',
      label: '',
      hidden: false,
      active: false,
      disabled: false,
      customClass: '', //自定义css类名
    },
  },

  {
    type: 'data-table',
    category: 'container',
    icon: 'data-table',
    options: {
      name: '',
      label: 'data-table',
      hidden: false,
      autoLoad: true,
      rowSpacing: 8,
      tableHeight: '300px', //高度，百分比或像素
      tableWidth: '100%', //宽度，百分比或像素宽度
      customClass: '', //自定义css样式
      stripe: true, // 是否斑马线
      showIndex: false, // 是否显示行号列
      showCheckBox: true, // 是否显示复选框列
      showPagination: true, // 是否分页
      paginationAlign: 'left',
      smallPagination: false, //是否显示小型分页
      showSummary: false, // 是否合计
      border: true, // 是否带有纵向边框（拖拽）
      tableSize: 'default', // 表格大小，large大、default中、small小
      autoColumnWidthDisabled: false,
      tableColumns: [
        //表格列信息

        /*{
          columnId: 14, prop: '~', headerFlag: true, label: '表头1', align: 'center', children: [
            {
              columnId: 15,
              prop: '~',
              headerFlag: true,
              label: '表头2',
              align: 'center',
              children: [
                {
                  columnId: 16,
                  prop: '~',
                  headerFlag: true,
                  label: '表头3',
                  align: 'center',
                  children: []
                }
              ]
            }
          ]
        },*/
      ],
      showButtonsColumn: false, // 是否显示操作列
      rowEditType: TABLE_EDIT_TYPE.ROW, //编辑的方式，dialoag:弹窗 row:行编辑
      buttonsColumnFixed: 'right',
      buttonsColumnTitle: '操作',
      buttonsColumnWidth: 120,
      operationButtons: [
        {
          // 操作按钮配置
          name: 'detail',
          label: '详情',
          type: 'text',
          size: 'small',
          round: false,
          hidden: true,
          disabled: false,
        },
        {
          name: 'edit',
          label: '编辑',
          type: 'text',
          size: 'small',
          round: false,
          hidden: false,
          disabled: false,
        },
        {
          name: 'delete',
          label: '删除',
          type: 'text',
          size: 'small',
          round: false,
          hidden: false,
          disabled: false,
        },
      ],
      pagination: {
        // 分页信息
        currentPage: 1,
        pageSizes: [10, 15, 20, 30, 50, 100, 200],
        pageSize: 20,
        total: 0,
      },
      dsEnabled: false, //是否使用数据源数据
      dsName: '', //数据源名称
      dataSetName: '', //数据集名称
      treeDataEnabled: false, //是否允许加载树形数据
      rowKey: 'id', //树形数据数据row-key
      childrenKey: 'children', //树形数据children
      service: {
        enabled: true,
        id: '',
      },
      tableData: [
        //表格数据
      ],

      onCreated: '',
      onMounted: '',
      onDataLoaded: '',
      //分页大小改变事件
      onPageSizeChange: '',
      //当前页改变事件
      onCurrentPageChange: '',
      //列排序改变事件（必须设置为后端排序才生效）
      onSortChange: '',
      //选择项改变事件
      onCurrentChange: '',
      //多选时，全选事件
      onCheckAllChange: '',
      //多选改变时触发
      onSelectionChange: '',

      //设置操作按钮是否隐藏回调事件（必须有返回值）
      // onHideOperationButton: '',
      //设置操作按钮是否禁用回调事件（必须有返回值）
      // onDisableOperationButton: '',
      //设置操作按钮文本回调事件（必须有返回值）
      // onGetOperationButtonLabel: '',
      //操作按钮点击事件
      onOperationButtonClick: '',

      // onHeaderClick: '',
      onRowClick: '',
      onRowDoubleClick: '',
      /*onCellClick: '',
      onCellDoubleClick: '',*/

      //获取行样式名称事件
      onGetRowClassName: '',
      //合并行、列事件
      onGetSpanMethod: '',
    },
  },

  {
    type: 'tree',
    icon: 'tree',
    category: 'container',
    widgetList: [],
    options: {
      name: 'tree',
      label: '',
      filter: true, // 过滤框
      draggable: false, // 可拖拽
      defaultExpandAllNode: true, // 默认展开所有节点
      selectClearAllNode: true, // 显示展开/收缩所有节点功能
      expandRetractAllNode: true, // 显示展开/收缩所有节点功能
      showCheckBox: true, // 展示复选框
      expandOnClickNode: true, // 点击节点收缩
      lazy: false, // 懒加载
      treeDataEdit: true, // 编辑树节点数据
      checkStrictly: false, //禁止父子节点选择联动
      nodeEdit: false, // 节点编辑
      size: 'default',
      disabled: false,
      hidden: false,
      dsEnabled: false, //是否使用数据源数据
      dsName: '', //数据源名称
      dataSetName: '', //数据集名称
      service: {
        enabled: true, //是否使用服务
        name: '',
      },
      optionItems: [],
      labelKey: 'label',
      valueKey: 'value',
      parentKey: 'parentCode',
      dataKey: '',
      autoLoad: true,
      //-------------------
      onCreated: '',
      onMounted: '',
      onNodeClick: '',
      onNodeCheck: '',
      onNodeContextmenu: '',
      onCheckChange: '',
      treeData: [
        {
          label: '一级 1',
          children: [
            {
              label: '二级 1-1',
              children: [
                {
                  label: '三级 1-1-1',
                },
              ],
            },
          ],
        },
        {
          label: '一级 2',
          children: [
            {
              label: '二级 2-1',
              children: [
                {
                  label: '三级 2-1-1',
                },
              ],
            },
            {
              label: '二级 2-2',
              children: [
                {
                  label: '三级 2-2-1',
                },
              ],
            },
          ],
        },
        {
          label: '一级 3',
          children: [
            {
              label: '二级 3-1',
              children: [
                {
                  label: '三级 3-1-1',
                },
              ],
            },
            {
              label: '二级 3-2',
              children: [
                {
                  label: '三级 3-2-1',
                },
              ],
            },
          ],
        },
      ],
    },
  },

  {
    type: 'button-group', // 按钮组
    icon: 'button-group',
    category: 'container',
    widgetList: [],
    options: {
      name: '',
      size: 'default',
      hidden: false,
      disabled: false,
      customClass: '',
      buttons: [
        {
          name: 'detail',
          label: '详情',
          icon: '',
          type: '',
          round: false,
          hidden: true,
          disabled: false,
        },
        {
          name: 'edit',
          label: '编辑',
          icon: '',
          type: '',
          round: false,
          hidden: false,
          disabled: false,
        },
        {
          name: 'delete',
          label: '删除',
          icon: '',
          type: '',
          round: false,
          hidden: false,
          disabled: false,
        },
      ],
      onCreated: '',
      onMounted: '',
      onButtonGroupClick: '',
    },
  },

  {
    type: 'object-group',
    category: 'container',
    icon: 'object-group',
    widgetList: [],
    options: {
      name: '',
      objectName: 'testObj',
      hidden: false,
      customClass: '',
      onCreated: '',
      onMounted: '',
    },
  },

  {
    type: 'vf-dialog',
    category: 'container',
    icon: 'vf-dialog',
    widgetList: [],
    options: {
      name: '',
      title: '',
      width: '50%',
      childForm: false,
      childFormCode: '',
      fullscreen: false,
      showModal: true,
      showClose: true,
      closeOnClickModal: false,
      closeOnPressEscape: false,
      center: false,
      readMode: false,
      disabledMode: false,
      service: {
        enabled: false,
        key: [],
      },
      okButtonLabel: '',
      okButtonHidden: false,
      cancelButtonLabel: '',
      cancelButtonHidden: false,
      onOkButtonClick: '',
      onCancelButtonClick: '',
      onDialogOpened: '',
      onDialogBeforeClose: '',
    },
  },

  {
    type: 'vf-drawer',
    category: 'container',
    icon: 'vf-drawer',
    widgetList: [],
    options: {
      name: '',
      title: '',
      size: '50%',
      showModal: true,
      showClose: true,
      closeOnClickModal: false,
      closeOnPressEscape: false,
      direction: 'rtl',
      readMode: false,
      disabledMode: false,
      childForm: false,
      childFormCode: '',
      service: {
        enabled: true,
        key: [],
      },
      okButtonLabel: '',
      okButtonHidden: false,
      cancelButtonLabel: '',
      cancelButtonHidden: false,
      onOkButtonClick: '',
      onCancelButtonClick: '',
      onDrawerOpened: '',
      onDrawerBeforeClose: '',
    },
  },
]
// Define types for each container's options
export type GridOptions = typeof containers[0]['options']
export type GridColOptions = typeof containers[1]['options']
export type GridAdvOptions = typeof containers[2]['options']
export type GridAdvColOptions = typeof containers[3]['options']
export type TableOptions = typeof containers[4]['options']
export type TabOptions = typeof containers[5]['options']
export type SubFormOptions = typeof containers[6]['options']
export type GridSubFormOptions = typeof containers[7]['options']
export type TableCellOptions = typeof containers[8]['options']
export type TabPaneOptions = typeof containers[9]['options']
export type DataTableOptions = typeof containers[10]['options']
export type TreeOptions = typeof containers[11]['options']
export type ButtonGroupOptions = typeof containers[12]['options']
export type ObjectGroupOptions = typeof containers[13]['options']
export type VfDialogOptions = typeof containers[14]['options']
export type VfDrawerOptions = typeof containers[15]['options']

// Define types for each complete container configuration
export type Grid = typeof containers[0]
export type GridCol = typeof containers[1]
export type GridAdv = typeof containers[2]
export type GridAdvCol = typeof containers[3]
export type Table = typeof containers[4]
export type Tab = typeof containers[5]
export type SubForm = typeof containers[6]
export type GridSubForm = typeof containers[7]
export type TableCell = typeof containers[8]
export type TabPane = typeof containers[9]
export type DataTable = typeof containers[10]
export type Tree = typeof containers[11]
export type ButtonGroup = typeof containers[12]
export type ObjectGroup = typeof containers[13]
export type VfDialog = typeof containers[14]
export type VfDrawer = typeof containers[15]


// Define a type for all container widget types
export type ContainerWidgetType =
  | 'grid'
  | 'grid-col'
  | 'grid-adv'
  | 'grid-adv-col'
  | 'table'
  | 'tab'
  | 'sub-form'
  | 'grid-sub-form'
  | 'table-cell'
  | 'tab-pane'
  | 'data-table'
  | 'tree'
  | 'button-group'
  | 'object-group'
  | 'vf-dialog'
  | 'vf-drawer'

// Define a constant object for container widget types
// This can be used instead of string literals for better type safety and autocomplete
export const CONTAINER_TYPES: Record<ContainerWidgetType, ContainerWidgetType> = {
  'grid': 'grid',
  'grid-col': 'grid-col',
  'grid-adv': 'grid-adv',
  'grid-adv-col': 'grid-adv-col',
  'table': 'table',
  'tab': 'tab',
  'sub-form': 'sub-form',
  'grid-sub-form': 'grid-sub-form',
  'table-cell': 'table-cell',
  'tab-pane': 'tab-pane',
  'data-table': 'data-table',
  'tree': 'tree',
  'button-group': 'button-group',
  'object-group': 'object-group',
  'vf-dialog': 'vf-dialog',
  'vf-drawer': 'vf-drawer',
}


